package com.brillilab.service.core.instrument.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.common.utils.TimeUtil;
import com.brillilab.dao.mapper.instrument.InstrumentMaintenanceMapper;
import com.brillilab.domain.po.instrument.Instrument;
import com.brillilab.domain.po.instrument.InstrumentAppointment;
import com.brillilab.domain.po.instrument.InstrumentMaintenance;
import com.brillilab.domain.vo.instrument.MaintenanceVo;
import com.brillilab.service.core.instrument.IInstrumentMaintenanceService;
import com.brillilab.service.logic.instrument.converter.MaintenanceConverter;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 仪器维护 服务实现类
 * </p>
 *
 * @author WuMenghao
 * @since 2019-06-12
 */
@Service
@Data
public class InstrumentMaintenanceServiceImpl implements IInstrumentMaintenanceService {

    @Resource
    private InstrumentMaintenanceMapper instrumentMaintenanceMapper;

    @Override
    public List<InstrumentMaintenance> selectList(Long instrumentId) {
        return instrumentMaintenanceMapper.selectList(
                new LambdaQueryWrapper<InstrumentMaintenance>()
                        .eq(InstrumentMaintenance::getInstrumId,instrumentId));
    }

    @Override
    public int deleteByInstrumentId(Long instrumentId) {
        return instrumentMaintenanceMapper.delete(
                new LambdaQueryWrapper<InstrumentMaintenance>()
                        .eq(InstrumentMaintenance::getInstrumId,instrumentId));
    }

    /**
     * 判断预约是否与维护时间重合
     *
     * @param appointment
     * @param year
     * @param month
     * @param date
     * @param dayOfWeek
     * @param maintenance
     * @return
     */
    @Override
    public boolean compareMaintenanceWhitAppointment(InstrumentAppointment appointment,int year,int month,int date,
                                                     int dayOfWeek,InstrumentMaintenance maintenance) {
        MaintenanceVo e=MaintenanceConverter.maintenanceTomaintenanceVo(maintenance);
        // 每周维护
        if(e.getType() == 0){
            if(dayOfWeek > e.getStartDay() && dayOfWeek < e.getEndDay()){
                return true;
            }

            if(e.getStartDay().equals(e.getEndDay()) && e.getStartDay().equals(dayOfWeek)){
                return isAppointmentCoincidenceWithMaintenance(appointment,year,month,date,e);
            } else {
                if(dayOfWeek == e.getStartDay()){
                    Date starttime=TimeUtil.getDirectDate(year,month,date,e.getStartTime());
                    if(starttime.getTime() <= appointment.getEndTime().getTime()){
                        return true;
                    }
                }

                if(dayOfWeek == e.getEndDay()){
                    Date endTime=TimeUtil.getDirectDate(year,month,date,e.getEndTime());
                    if(endTime.getTime() >= appointment.getStartTime().getTime()){
                        return true;
                    }
                }
            }
        }

        // 每月维护
        if(e.getType() == 1){
            if(date > e.getStartDay() && date < e.getEndDay()){
                return true;
            }

            if(e.getStartDay().equals(e.getEndDay()) && e.getStartDay().equals(date)){
                return isAppointmentCoincidenceWithMaintenance(appointment,year,month,date,e);
            } else {
                if(date == e.getStartDay()){
                    Date starttime=TimeUtil.getDirectDate(year,month,date,e.getStartTime());
                    if(starttime.getTime() <= appointment.getEndTime().getTime()){
                        return true;
                    }
                }
                if(date == e.getEndDay()){
                    Date endTime=TimeUtil.getDirectDate(year,month,date,e.getEndTime());
                    if(endTime.getTime() >= appointment.getStartTime().getTime()){
                        return true;
                    }
                }
            }
        }

        // 每年维护
        if(e.getType() == 2){

            if(month > e.getStartMonth() && month < e.getEndMonth()){
                return true;
            }

            if(e.getStartMonth().equals(e.getEndMonth()) && e.getStartDay().equals(e.getEndDay())
                    && e.getStartDay().equals(date)){
                return isAppointmentCoincidenceWithMaintenance(appointment,year,month,date,e);
            } else {
                if(month == e.getStartMonth()){
                    if(date > e.getStartDay()){
                        return true;
                    }
                    if(date == e.getStartDay()){
                        Date starttime=TimeUtil.getDirectDate(year,e.getStartMonth(),e.getStartDay(),
                                e.getStartTime());
                        if(starttime.getTime() <= appointment.getEndTime().getTime()){
                            return true;
                        }
                    }
                }

                if(month == e.getEndMonth()){
                    if(date < e.getEndDay()){
                        return true;
                    }
                    if(date == e.getEndDay()){
                        Date endtime=TimeUtil.getDirectDate(year,e.getEndMonth(),e.getEndDay(),e.getEndTime());
                        if(endtime.getTime() >= appointment.getStartTime().getTime()){
                            return true;
                        }
                    }
                }
            }

        }

        return false;
    }

    @Override
    public int insert(InstrumentMaintenance maintenance) {
        return instrumentMaintenanceMapper.insert(maintenance);
    }

    @Override
    public InstrumentMaintenance selectById(Long id) {
        return instrumentMaintenanceMapper.selectById(id);
    }

    @Override
    public boolean deleteById(Long id) {
        return instrumentMaintenanceMapper.deleteById(id) >= 0;
    }

    /**
     * 判断预约是否与维护时间重合（同一天）
     *
     * @param appointment
     * @param year
     * @param month
     * @param date
     * @param maintenanceVo
     * @return
     */
    private boolean isAppointmentCoincidenceWithMaintenance(InstrumentAppointment appointment,int year,int month,
                                                            int date,MaintenanceVo maintenanceVo) {
        Date starttime2=TimeUtil.getDirectDate(year,month,date,maintenanceVo.getStartTime());
        Date endTime2=TimeUtil.getDirectDate(year,month,date,maintenanceVo.getEndTime());
        return TimeUtil.isCoincidence(appointment.getStartTime(),starttime2,appointment.getEndTime(),endTime2);

    }

}
